const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    banner:[],
    page:1,
    pageSize:10,
    isEnd:false,
    contentlistdata:[],
    isloading:true,
    type:1,
    city:'全国',
    cangecity:"",
    isfixed:false,
    bannerheight:0,
  },
  onLoad(option) {
    this.bannerList()
    if(this.data.city && !this.data.cangecity){
      this.contentlist()
    }
    if(option.scene){
      app.globalData.promoterid=decodeURIComponent(option.scene).split('promoterId=')[1];
      console.log(app.globalData.promoterid)
    }

  },
  onShow(){
    if(this.data.cangecity && this.data.cangecity!=this.data.city){
      this.setData({
        city:this.data.cangecity
      })
      this.setData({
        page:1,
        pageSize:10,
        isEnd:false,
        contentlistdata:[],
      })
      this.contentlist()
    }
    // if(!this.data.city){
    //   this.getaddress()
    // }
  },
  bannerList(){
    Controller.bannerList({source:1}).then(res=>{
      if(res.data.baseData){
        this.setData({
          banner:res.data.baseData
        })
        var that=this;
        setTimeout(res=>{
          that.getHeght();
        },1000)
      }
    })
  },
  tabs(e){
    this.setData({
      type:e.currentTarget.dataset.type,
      page:1,
      pageSize:10,
      isEnd:false,
      contentlistdata:[],
    })
    this.contentlist()
  },
  getaddress(e) {
    console.log(e)
    var that=this;
      wx.getLocation({
        type: 'gcj02',
        success: function(res) {
          console.log(res)
          that.setData({
            lng:res.longitude,
            lat:res.latitude
          })
          that.loadCity()
        },
        fail: function(err) {
            that.contentlist()
        },
        complete: function(res) {
          wx.getSetting({
            success: (res) => {
              let authSetting = res.authSetting
              if (authSetting['scope.userLocation']) {
                // 已授权
              } else {
                wx.showModal({
                  title: '是否开启地理位置授权',
                  content: '',
                  success: res => {
                    if (res.confirm) {
                      wx.openSetting()
                    }
                  }
                })
              }
            }
          })
          
        },
      })
    // }
  },
  loadCity: function (longitude, latitude) {
    var longitude=this.data.lng;
    var latitude=this.data.lat;
    var that = this;
    wx.request({
      url: 'https://restapi.amap.com/v3/geocode/regeo',
      data: {
        key: 'ce583325ba4b00ec77b9237ee4a55196',
        location: longitude + "," + latitude,
        extensions: "all",
        s: "rsx",
        sdkversion: "sdkversion",
        logversion: "logversion"
 
      },
      success: function (res) {
        that.setData({
          city: res.data.regeocode.addressComponent.city
        })
        that.contentlist()
      },
      fail: function (res) {
        that.contentlist()
        console.log('获取地理位置失败')
      }
    })
  },
  golink(e){
    
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  golink1(e){
    var url=e.currentTarget.dataset.url;
    if (!wx.getStorageSync('logintoken')) {
      app.goLogin(false)
      return
    }
    if(url=='/pages/settlement/settlement'){
      Controller.checkAllowSettlement().then(res=>{
        console.log(res)
        if(res.code==0){
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel:false,
            confirmColor:'#F08519',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else{
          if(res.code==-3){
            wx.navigateTo({
              url:"/pages/userCompany/userCompany"
            });
          }else{
            wx.showModal({
              title: '提示',
              content: res.msg,
              showCancel:false,
              confirmColor:'#F08519',
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      }).catch(res=>{
        
      })
      return
    }
    if(url=='/pages/advance/advance'){
      Controller.checkAllowAdvancePay().then(res=>{
        if(res.code==0){
          wx.navigateTo({
              url:"/pages/advance/advance"
            });
        }else{
          if(res.code==-3){
            wx.navigateTo({
              url:"/pages/userCompany/userCompany"
            });
          }else{
            wx.showModal({
              title: '提示',
              content: res.msg,
              showCancel:false,
              confirmColor:'#F08519',
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
          
        }
      }).catch(res=>{
      })
      return
    }
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  golink2(e){
    var item=e.currentTarget.dataset.item;
    wx.navigateTo({
      url:"/pages/detail/detail?id="+item.rid
    });
  },
  godetail(e){
    var item=e.currentTarget.dataset.item;
    wx.navigateTo({
      url:"/pages/detail/detail?id="+item.id
    });
    
  },
  contentlist(city){
    var data={
      keyword:"",
      isHot:this.data.type==1?1:'',
      page:this.data.page,
      pageSize:this.data.pageSize,
      companyId:'',
      city:'',
    }
    if(this.data.type==1 || this.data.type==2){
      var citynew=city?city:this.data.city;
      if(this.data.type==2){
        data.city=citynew=='全国'?'':citynew
      }
      Controller.joblist(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
          })
          if(res.data.baseData.lastPage){
            this.setData({
              isEnd:res.data.baseData.lastPage
            })
          }
        }else{
          if(res.data.baseData.lastPage){
            this.setData({
              isEnd:res.data.baseData.lastPage
            })
          }
          if(this.data.type==2){
            wx.showToast({
              title:'当前城市无热门招聘',
              icon:"none"
            })
            
            if(this.data.city!='全国')
            var that=this;
            that.setData({
              page:1,
              pageSize:10,
              isEnd:false,
              contentlistdata:[],
            })
            setTimeout(()=>{
              that.contentlist('全国')
            },1500)
          }
        }
        
        this.setData({
          isloading:false
        })
      })
    }else{
      Controller.companylist(data).then(res=>{
        if(res.data.baseData && res.data.baseData.list.length>0){
          this.setData({
            contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
          })
        }
        if(res.data.baseData.lastPage){
          this.setData({
            isEnd:res.data.baseData.lastPage
          })
        }
        this.setData({
          isloading:false
        })
      })
    }
  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.contentlist()
    }
    
  },
  getHeght(){  //获取屏幕高度
    let query = wx.createSelectorQuery();
    query.select('.banner').boundingClientRect(rect => {
      console.log(rect)
        //获取到元素
        let height = rect.height;
        var top=rect.top;

        this.setData({
            bannerheight:top+height
        })
    }).exec();
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    var top=e.scrollTop;
    if(top>=this.data.bannerheight){
      this.setData({
        isfixed:true
      })
    }else{
      this.setData({
        isfixed:false
      })
    }
  },
  onShareAppMessage: function (e) {
    
  },
  onShareTimeline: function(res){
    
  },

})